<template>
  <!-- 商品列表 -->
  <div class="mall_page">
    <div class="mall_content gl_clear">
      <!-- 菜单 -->
      <ul class="mall_menu gl_fl">
        <li
          v-for="(item, index) in menuInfo"
          :key="index"
          :class="{active:menu.active === index}"
          class="mall_menu_item gl_ellipsis"
          @click="changeMenu(index)"
        >
          {{ item.name }}
        </li>
      </ul>

      <!-- 商品 -->
      <div class="mall_goods gl_clear">
        <!-- 推荐商品 -->
        <div v-if="recommendInfo.length" class="recommend gl_fr">
          <div class="goods_title gl_clear">
            <span class="gl_fl">推荐商品</span>
          </div>
          <ul class="gl_py_15">
            <li v-for="(item, index) in recommendInfo" :key="index" class="goods_item gl_mb_20" @click="jumpToGoodsDetail(item.productId)">
              <div class="goods_item_imgWrap">
                <template v-if="item.product.images[0]">
                  <img class="goods_item_img" :src="getStaticFileUrl(item.product.images[0].imageUrl)">
                </template>
              </div>
              <div class="goods_item_price">
                <span class="goods_item_yuan">￥</span>
                <span>{{ formatMoney(item.product.price) }}</span>
              </div>
              <div class="goods_item_desc">{{ item.product.name }}</div>
            </li>
          </ul>
        </div>
        <!-- 主商品 -->
        <div class="main_goods gl_clear">
          <template>
            <div class="gl_mb_20">
              <div class="goods_title gl_clear">
                <span class="gl_fl">搜索结果</span>
              </div>
              <ul class="minus20 gl_py_15 gl_clear">
                <li v-for="(jtem, jndex) in goodsRequest" :key="jndex" class="goods_item mr_18 gl_mb_20 gl_fl" @click="jumpToGoodsDetail(jtem.id)">
                  <div class="goods_item_imgWrap">
                    <img class="goods_item_img" :src="getStaticFileUrl(jtem.images[0].imageUrl)">
                  </div>
                  <div class="goods_item_price">
                    <span class="goods_item_yuan">￥</span>
                    <span>{{ formatMoney(jtem.price) }}</span>
                  </div>
                  <div class="goods_item_desc">{{ jtem.name }}</div>
                </li>
                <li v-show="!goodsRequest.length" class="no_goods">
                  {{ searchLoading ? '正在搜索商品...' : '暂无商品' }}
                </li>
              </ul>
            </div>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  api_GET_MallProduct // 获取商品列表
} from '@/extend/api/shoppingmallswaggerApi/app.js';

export default {
  props: {
    shopInfo: {
      type: Object,
      default() {
        return {};
      }
    },
    menuInfo: {
      type: Array,
      default() {
        return [];
      }
    },
    recommendInfo: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data () {
    return {
      menu: {
        active: 0
      },
      goodsRequest: [], // 获取到的全部商品列表
      key: '', // 搜索关键字
      searchLoading: true
    };
  },
  computed: {
    activeMenu () {
      return this.menuInfo[this.menu.active];
    }
  },
  beforeRouteUpdate(to, from, next) {
    this.getGoodsList({
      StoreId: this.shopInfo.id,
      Filter: to.query.key,
      ProductStatusCodes: ['OnShelf'], // 销售中
      IncludeImages: true,
      IncludeGroup: true,
      IncludeStatus: true
    });
    next();
  },
  created () {
    this.key = this.$route.query.key;
    this.getGoodsList({
      StoreId: this.shopInfo.id,
      Filter: this.key,
      ProductStatusCodes: ['OnShelf'], // 销售中
      IncludeImages: true,
      IncludeGroup: true,
      IncludeStatus: true
    });
  },
  methods: {
    // 切换菜单
    changeMenu (index) {
      this.menu.active = index;
      const _query = Object.assign({}, this.$route.query);
      const _parms = this.$route.params;
      _query.active = index;
      delete _query.key;

      if (this.shopInfo.route) {
        this.$router.push({
          name: 'MallHome',
          query: _query,
          params: _parms
        });
      }
    },
    // 获取商品列表
    getGoodsList (qo) {
      this.searchLoading = true;
      api_GET_MallProduct(qo)
        .then(
          res => {
            this.searchLoading = false;
            if (res && res.items) {
              this.goodsRequest = res.items || [];
            }
          }
        )
        .catch(
          () => {
            this.searchLoading = false;
          }
        );
    },
    // 跳转至商品详情
    jumpToGoodsDetail (goodsId) {
      const _parms = this.$route.params;
      const _query = Object.assign({}, this.$route.query);
      delete _query.key;
      _parms['goodsId'] = goodsId;
      this.$router.push({
        name: 'MallDetail',
        params: _parms,
        query: _query
      });
    },
    // 格式化货币格式 （分）
    formatMoney (num) {
      if (typeof num === 'number' && !Number.isNaN(num)) {
        // num 是数字类型 且不是 NaN
        let strNum = String(num); // 金额文本类型
        if (num >= 0) {
          strNum = strNum.padStart(3, '0'); // 用0填充保证最小三位数
          const _floatNum = strNum.slice(-2);
          if (Number(_floatNum)) {
            return strNum.slice(0, -2) + '.' + _floatNum; // 金额拼接
          } else {
            return strNum.slice(0, -2);
          }
        } else {
          const _mark = strNum.slice(0, 1); // 负号
          const _noMark = strNum.slice(1); // 无负号数值
          strNum = _noMark.padStart(3, '0'); // 用0填充保证最小三位数
          const _floatNum = strNum.slice(-2);
          if (Number(_floatNum)) {
            return _mark + strNum.slice(0, -2) + '.' + _floatNum; // 金额拼接
          } else {
            return _mark + strNum.slice(0, -2);
          }
        }
      } else {
        return '';
      }
    },
    // 将元转化为分
    yuanToFen(moneyStr) {
      const _moneyStr = String(moneyStr);
      const yuan = Number(_moneyStr.split('.')[0]) || 0;
      const fen = Number(_moneyStr.split('.')[1]) || 0;
      return yuan * 100 + fen;
    },
    // 获取静态文件
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    }
  }
};
</script>

<style lang="scss" scoped>
  .mall_goods {
    padding-top: 20px;
    margin-left: 210px;
  }

  .minus20 {
    margin-right: -20px;
    padding-left: 5px;
  }
  .mr_18 {
    margin-right: 18px;
  }
  .recommend {
    margin-left: 25px;
  }
  .no_goods {
    color: #C0C4CC;
    line-height: 36px;
    font-size: 14px;
    text-align: center;
  }
</style>
